@gilbarbara/helpers
Collection of useful functions
Usage
npm i @gilbarbara/helpers
import { unique } from '@gilbarbara/helpers';
const password = unique(24, { includeSymbols: true });
console.log(password);
API
Arrays
createArray(size: number, start: number = 1): number[]
Create a sequential array of numbers.
getRandomItem<T>(input: T[]): T
Get a random item from an array.
quickSort<T extends string | number>(input: T[]): T[]
Sort an array of numbers using a quick sort algorithm.
shuffle<T = unknown>(input: T[]): T[]
Shuffle an array using the Fisher-Yates algorithm.
sortByLocaleCompare(key?: string, options?: Intl.CollatorOptions & { descending?: boolean }): SortFunction
Returns a sort function with localeCompare comparison.
Type Definition
interface SortFunction<T = string> {
(left: PlainObject, right: PlainObject): number;
(left: T, right: T): number;
}
Example
const strings = ['Mãe', 'limão', 'cachê', 'tião', 'amô', 'côncavo'];
strings.sort(sortByLocaleCompare());
const objects = [{ key: 'réservé' }, { key: 'Premier' }, { key: 'Cliché' }, { key: 'communiqué' }, { key: 'café' }, { key: 'Adieu' }];
objects.sort(sortByLocaleCompare('key', { descending: true }));
sortByPrimitive<T extends number | boolean>(key?: string, descending?: boolean = false): SortFunction
Returns a sort function with primitive values comparison.
Type Definition
interface SortFunction<T = string> {
(left: PlainObject, right: PlainObject): number;
(left: T, right: T): number;
}
Example
const objects = [{ cycle: 3, status: true }, { cycle: 1, status: false }, { cycle: 3, status: true }, { cycle: 4, status: false }];
objects.sort(sortByPrimitive('status', true));
sortComparator(left: string | number, right: string | number): number
Basic sort comparator.
splitIntoChunks<T>(array: T[], chunkSize: number = 25): T[][]
Split an array into chunks.
Async
ASYNC_STATUS
A constant with possible async statuses.
cors(data: any, statusCodeOrOptions: number | CorsOptions = 200): CorsResponse
Returns a CORS response.
Type Definition
type HttpMethods = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
interface CorsOptions {
allowCredentials?: boolean;
allowedHeaders?: string[];
methods?: HttpMethods[];
origin?: string;
responseHeaders?: Record<string, string>;
statusCode?: number;
}
interface CorsResponse {
body: string;
headers: Record<string, string>;
statusCode: number;
}
poll(condition: () => boolean, options?: PollOptions): Promise<void>
Awaits for the condition to be true based on the options.
Type Definition
interface PollOptions {
delay?: number;
maxRetries?: number;
}
request<D = any>(url: string, options?: RequestOptions): Promise<D>
Perform an async request.
Type Definition
type HttpMethods = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
interface RequestOptions {
body?: any;
headers?: PlainObject;
method?: HttpMethods;
}
You will need a polyfill if your Node version does not support fetch.
sleep(seconds?: number = 1): Promise<void>
Block async execution for X seconds.
Date
isIsoDate(input?: string): boolean
Check if the input is an ISO date.
isoDate(input?: string | number): string
Returns an ISO date.
isValidDate(input: string | number | Date): boolean
Check if the input is a valid date.
now(): number
Returns a unix timestamp (seconds since 1970-01-01 00:00 UTC).
timeSince(input: Date | string | number, options?: TimeSinceOptions): string
Returns how much time has passed since the input date.
You can change the locale with the options.
If the plural form just adds an s
to the end, you don't need to pass it. It will add it automatically.
Type Definition
interface TimeSinceOptions {
day?: string;
days?: string;
hour?: string;
hours?: string;
minute?: string;
minutes?: string;
month?: string;
months?: string;
prefix?: string;
second?: string;
seconds?: string;
skipWeeks?: boolean;
suffix?: string;
week?: string;
weeks?: string;
year?: string;
years?: string;
}
Example
timeSince(twoDaysAgo)
timeSince(twoWeeksAgo, { skipWeeks: true })
timeSince(twoDaysAgo, { day: 'Tag', days: 'Tage', prefix: 'Vor', suffix:'' })
timeSince(twoWeeeksAgo, { suffix: 'atrás', week: 'semana' })
timestamp(input?: Date | string): number
Get the timestamp for a date.
Devices
isDarkMode(): boolean
Detect if the device is in dark mode.
isTouchDevice(): boolean
Detect if the device supports touch events.
prefersReducedMotion(): boolean
Detect if the user prefers reduced motion.
Formatters
formatBoolean(input: boolean): 'Yes' | 'No'
Format the boolean into Yes / No.
formatCPF(input: string): string
Format the string into a CPF.
formatDateLocale(input: string, options?: FormatDateLocaleOptions): string
Format the ISO date string using locale.
Type Definition
interface FormatDateLocaleOptions {
locale?: string;
showTime?: boolean;
}
formatMoney(input: number, options?: FormatMoneyOptions): string
Format the number into a money string.
Type Definition
interface FormatMoneyOptions {
decimalChar?: ',' | '.';
showCents?: boolean;
symbol?: string;
thousandsChar?: ',' | '.';
}
formatPhoneBR(input: string): string
Format the string into a brazilian phone.
formatPhoneUS(input: string): string
Format the string into a US phone.
formatPostalCodeBR(value: string): string
Format the string into a brazilian zip code.
Functions
demethodize(fn: Function): Function
Decouple the method from the prototype.
measureExecutionTime<T = any>(callback: Function): Promise<T>
Measure function execution time.
noop(): void
An empty function that does nothing.
once<T extends (...arguments_: Array<any>) => any>(fn: T): T
Creates a function that will only be called once.
Repeat calls return the value of the first invocation.
pipe<T>(...functions: Array<(argument: T) => T>)
Combine multiple functions into one.
The output of each function is passed as the input to the next.
Misc
conditional<TReturn>(cases: Array<Case<TReturn>>, defaultCase?: () => TReturn): TReturn | undefined
A replacement for switch statements with dynamic expressions cases.
Type Definition
type Case<T = void> = [boolean, () => T];
Example
let type: string = '';
conditional(
[
[type === 'a', () => console.log('a')],
[[type].includes('b'), () => console.log('b')],
[type === 'c', () => console.log('c')],
],
() => console.log('default'),
);
copyToClipboard(input: string): Promise<boolean>
Copy the string to the clipboard.
Example
const toLocaleString = demethodize(Number.prototype.toLocaleString);
const numbers = [2209.6, 124.56, 1048576];
numbers.map(toLocaleString);
getDataType(input: unknown, toLowerCase = false): string
Get the data type of variable.
Example
getDataType([1, 2, 3]);
getDataType(/\d+/);
getDataType(() => {}, true)
invariant(condition: any, message: string): asserts condition
Check if the value is truthy. Otherwise, it will throw.
isJSON(input: string): boolean
Check if a string is a valid JSON.
isRequired(input?: string = 'parameter', type: Constructable = TypeError): void
Throws an error of the Constructable type.
Example
function exec(input: string = isRequired('input')) {}
exec()
function evaluate(input: string = isRequired('input', SyntaxError)) {}
exec()
logger(type: string, title: string, data: any, options?: LoggerOptions): void
Log grouped messages to the console.
Type Definition
interface LoggerOptions {
collapsed?: boolean;
hideTimestamp?: boolean;
skip?: boolean;
typeColor?: string;
}
on<T extends Window | Document | HTMLElement | EventTarget>(
target: T | null,
...rest: Parameters<T['addEventListener']> | [string, Function | null, ...any]
): void
Add the event listener to the target
off<T extends Window | Document | HTMLElement | EventTarget>(
target: T | null,
...rest: Parameters<T['removeEventListener']> | [string, Function | null, ...any]
): void
Remove the event listener from the target
nullify<T>(value: T): T | null
Returns the value or null.
popupCenter(url: string, title: string, width: number, height: number): Window | null
Open a centered popup window and returns it.
px(value: string | number | undefined): string | undefined
Convert a number or numeric value to px.
Otherwise, return the value.
unique(length?: number = 8, options?: UniqueOptions): string
Returns a random string.
Type Definition
interface UniqueOptions {
includeLowercase?: boolean;
includeNumbers?: boolean;
includeSymbols?: boolean;
includeUppercase?: boolean;
}
uuid(): string
Returns a UUID v4 string.
Numbers
ceil(input: number, digits?: number = 2): number
Ceil decimal numbers.
clamp(value: number, min?: number = 0, max?: number = 100): number
Limit the number between ranges.
floor(input: number, digits?: number = 2): number
Floor decimal numbers.
pad(input: number, digits?: number = 2): string
Pad a number with zeros.
percentage(input: number, total: number, digits?: number = 2): number
Calculate the percentage of a number in relation to the total.
randomInt(min?: number = 0, max?: number = 10): number
Returns a random integer.
round(input: number, digits?: number = 2): number
Round decimal numbers.
Objects
cleanUpObject(input: PlainObject): PlainObject
Remove properties with undefined values from an object.
getNestedProperty(input: PlainObject | any[], path: string): any
Get a nested property inside an object or array.
Example
getNestedProperty({ children: { letters: ['a', 'b', 'c'] } }, 'children.letters');
getNestedProperty({ children: { letters: ['a', 'b', 'c'] } }, 'children.letters.1');
getNestedProperty([{ a: 5 }, { a: 7 }, { a: 10 }], '0.a');
You may also use a wildcard (+) to get multiple array values:
getNestedProperty([{ a: 5 }, { a: 7 }, { a: 10 }], '+.a');
getNestedProperty({ children: [{ a: 5 }, { a: 7 }, { a: 10 }] }, 'children.+.a');
invertKeys(input: PlainObject): PlainObject
Invert object key and value.
keyMirror(input: PlainObject): PlainObject
Set the key as the value.
mergeProps<TDefaultProps, TProps>(defaultProps: TDefaultProps, props: TProps): TProps
Merges the defaultProps with literal values with the incoming props, removing undefined values from it that would override the defaultProps.
The result is a type-safe object with the defaultProps as required properties.
objectEntries<T extends PlainObject<any>>(input: T): Array<{ [K in keyof T]-?: [K, T[K]] }[keyof T]>
Type-safe Object.entries().
objectKeys<T extends PlainObject<any>(input: T): Array<keyof T>
Type-safe Object.keys().
objectToArray(input: PlainObject, includeOnly?: string): Array<PlainObject>
Convert an object to an array of objects.
omit<T extends PlainObject, K extends keyof T>(input: T, ...filter: K[]): PlainObject
Remove properties from an object.
pick<T extends PlainObject, K extends keyof T>(input: T, ...filter: K[]): PlainObject
Select properties from an object.
queryStringFormat(input: PlainObject, options?: QueryStringFormatOptions): string
Stringify a shallow object into a query string.
Type Definition
interface QueryStringFormatOptions {
addPrefix?: boolean;
encodeValuesOnly?: boolean;
encoder?: (uri: string) => string;
}
queryStringParse(input: string): PlainObject
Parse a query string.
sortObjectKeys(input: PlainObject): PlainObject
Sort object keys.
Statistics
mean(input: number[], precision?: number): number
Returns the average of two or more numbers.
median(input: number[]): number
Returns the median of two or more numbers.
mode(input: number[]): number
Returns the mode of two or more numbers.
Strings
capitalize(input: string): string
Capitalize the first letter.
cleanupHTML(input: string): string
Clean up HTML content.
cleanupNumericString(input: string): string
Clean up a numeric string.
cleanupURI(input: string): string
Clean up URI characters.
getInitials(input: string): string
Get initials from the name.
pluralize(singular: string, plural: string | undefined, quantity: number): string
Returns the singular or plural based on the quantity.
If the plural form just adds an s
to the end, you don't need to pass it. It will add it automatically.
removeAccents(input: string): string
Remove accents.
removeEmojis(input: string): string
Remove emojis.
removeEmptyTags(input: string): string
Remove empty HTML Tags (including whitespace).
removeNonPrintableCharacters(input: string): string
Remove non-printable ASCII characters.
removeTags(input: string): string
Remove HTML tags.
removeWhitespace(input: string): string
Remove whitespace.
slugify(input: string): string
Format the string into a slug.
Validators
isValidCPF(value: string): boolean
Check if the CPF is valid.
isValidEmail(value: string): boolean
Check if the email is valid.
@throws
validatePassword(password: string, options?: ValidatePasswordOptions): boolean
Validate password length and required characters.
interface ValidatePasswordOptions {
maxLength?: string;
maxLengthMessage?: string;
minLength?: string;
minLengthMessage?: string;
regex?: RegExp;
requiredCharactersMessage?: string;
}
License
MIT